package org.javaongems.client.stdctrls;

import org.javaongems.client.vcl.Control;
import org.javaongems.client.vcl.PropertiesReader;
import org.javaongems.client.vcl.PropertiesWriter;

/**
 * @author Ashin Wimalajeewa (ash)
 *
 * TODO: need to implement transparent property
 * TODO: need to work out how to make accelerator keys work
 */
public class Label extends Control {
	final static public String ALIGNMENT_PROP = "alignment";
	final static public String LAYOUT_PROP = "layout";
	final static public String TRANSPARENT_PROP = "transparent";
	final static public String ACCELCHAR_PROP = "accel-char";
	final static public String WORDWRAP_PROP = "word-wrap";

	final static public String DEFAULT_LAYOUT_STYLE = "baseline";
	
	protected int alignment = TEXT_ALIGNMENT_LEFT;
	protected String layout = DEFAULT_LAYOUT_STYLE;
	protected boolean accelChar = true;
	protected boolean transparent = false;
	protected boolean wordWrap = false;
	
	protected Control focusControl;
	
	protected void paintAttributeCaption() {
		// dont do anything with captions, labels use text instead
	}
	
	public void setAlignment(int a) {
		if (alignment == a)
			return;
		alignment = a;
		paintTextAlignment();
	}

	protected void paintTextAlignment() {
		if (!isReadyToPaint())
			return;
		domRenderer.setTextAlignment(alignment);
	}
	
	public int getAlignment() {
		return alignment;
	}
	
	public void setLayout(String l) {
		if (layout == l || layout.equals(l))
			return;
		layout = l;
		paintLayout();
	}

	protected void paintLayout() {
		if (!isReadyToPaint())
			return;
		domRenderer.setVerticalAlignment(layout);
	}
	
	public void setTransparent(boolean t) {
		if (transparent == t)
			return;
		transparent = t;
		paintTransparent();
	}
	
	protected void paintTransparent() {
		if (!isReadyToPaint())
			return;
		// TODO
	}
	
	public boolean isTransparent() {
		return transparent;
	}
	
	public void setAccelKey(boolean ak) {
		if (accelChar == ak)
			return;
		accelChar = ak;
		// TODO
	}
	
	public boolean isAccelKey() {
		return accelChar;
	}
	
	public void setWordWrap(boolean w) {
		if (wordWrap == w)
			return;
		wordWrap = w;
		paintWordWrap();
	}

	protected void paintWordWrap() {
		if (!isReadyToPaint())
			return;
		domRenderer.setWordWrap(wordWrap);
	}
	
	public boolean isWordWrap() {
		return wordWrap;
	}

	protected void writeProperties(PropertiesWriter wrtr) {
		super.writeProperties(wrtr);
		if (alignment != TEXT_ALIGNMENT_LEFT)
			wrtr.writeNumber(ALIGNMENT_PROP, alignment);
		if (!DEFAULT_LAYOUT_STYLE.equals(layout))
			wrtr.writeString(LAYOUT_PROP, layout);
		if (accelChar)
			wrtr.writeBoolean(ACCELCHAR_PROP, accelChar);
		if (transparent)
			wrtr.writeBoolean(TRANSPARENT_PROP, transparent);
		if (wordWrap)
			wrtr.writeBoolean(WORDWRAP_PROP, wordWrap);
	}
	
	protected void readProperties(PropertiesReader rdr) {
		super.readProperties(rdr);
		setAlignment((int)rdr.readNumber(ALIGNMENT_PROP, TEXT_ALIGNMENT_LEFT));
		setLayout(rdr.readString(LAYOUT_PROP, DEFAULT_LAYOUT_STYLE));
		setAccelKey(rdr.readBoolean(ACCELCHAR_PROP, true));
		setTransparent(rdr.readBoolean(TRANSPARENT_PROP, false));
		setWordWrap(rdr.readBoolean(WORDWRAP_PROP, false));
	}
}
